<?php
$path="C:/xampp/htdocs/00_JO/JOii/";
require_once ($path.'DataProvider/DataConfig.php');
//$databaseURL="localhost";
//$databaseName="JO";
//$databaseUName="JO";
//$databasePWord="";
class DataProvider {
    /**
     * Ejecuta un comando SQL y devuelve el recurso (resource) correspondiente
     * @param string $SQLCommand
     * @return resource
     */
    public static function executeSQL($SQLCommand){
        try{

        $dbURL  = DataConfig::databaseURL  ();
        $dbName = DataConfig::databaseName ();
        $dbUsr  = DataConfig::databaseUName();
        $dbPwd  = DataConfig::databasePWord();
        $JO_MySQL= new mysqli( $dbURL, $dbUsr, $dbPwd, $dbName );
        $JO_MySQL -> query( "SET NAMES 'UTF8'" );
        $JO_MySQL -> query( "SET SQL_SAFE_UPDATES=0" );
        $result = $JO_MySQL -> query ($SQLCommand);
        if(isset($result->num_rows)?$result->num_rows:false) return $result;
        if ($JO_MySQL->errno!=0){
            $errmsg= "<b style='color:red'>Begin Dataprovider Error:</b><br/>";
            $errmsg.= "SQLCommand:<span style='color:#FFAC30;'>".$SQLCommand."</span><br/>";
            $errmsg.= new Exception($JO_MySQL->error." - <span style='color:#FFAC30;'>".$SQLCommand."</span>", "1");
            $errmsg.= "<br/><b style='color:red'>End Dataprovider Error</b>";
            //echo "<a href='JO_00_integration.php?JO_unit=00_Error' style='color:red;' target='_blank'>Click Here To View This Error</a>";
            //throw new Exception($mysqli->error." - ".$SQLCommand, "1");
            if(!isset($_SESSION["MYSQL_ERR"])){
                //session_register("MYSQL_ERR");
            }
            $_SESSION["MYSQL_ERR"]=$errmsg;
            $JO_MySQL->close();
            return null;
        }
        } catch (Exception $exc) {
            
            $errmsg= "<b style='color:red'>Begin Dataprovider Error:</b><br/>";
            $errmsg.= "SQLCommand:<span style='color:#FFAC30;'>".$SQLCommand."</span><br/>";
            $errmsg.= $exc->getTraceAsString();
            $errmsg.= "<br/><b style='color:red'>End Dataprovider Error</b>";
            //echo "<a href='JO_00_integration.php?JO_unit=00_Error' style='color:red;' target='_blank'>Click Here To View This Error</a>";
            if(!isset($_SESSION["MYSQL_ERR"])){
                //session_register("MYSQL_ERR");
            }
            $_SESSION["MYSQL_ERR"]=$errmsg;
            $JO_MySQL->close();
            return null;
        }

        $JO_MySQL->close();
        
        return null;
    }
    public static function executeNoneSQL($SQLCommand){
        try {
            $dbURL  = DataConfig::databaseURL  ();
            $dbName = DataConfig::databaseName ();
            $dbUsr  = DataConfig::databaseUName();
            $dbPwd  = DataConfig::databasePWord();
            
            $JO_MySQL = new mysqli( $dbURL, $dbUsr, $dbPwd, $dbName );
            $JO_MySQL -> query( "SET NAMES 'UTF8'" );
            $result = $JO_MySQL -> query ($SQLCommand);
            $affected_rows=$JO_MySQL->affected_rows;//mysql_affected_rows();
            if ($JO_MySQL->errno!=0){
                $errmsg= "<b style='color:red'>Begin Dataprovider Error:</b><br/>";
                $errmsg.= "SQLCommand:<span style='color:#FFAC30;'>".$SQLCommand."</span><br/>";
                $errmsg.= new Exception($JO_MySQL->error." - <span style='color:#FFAC30;'>".$SQLCommand."</span>", "1");
                $errmsg.= "<br/><b style='color:red'>End Dataprovider Error</b>";
                //echo "<a href='JO_00_integration.php?JO_unit=00_Error' style='color:red;' target='_blank'>Click Here To View This Error</a>";
                //throw new Exception($mysqli->error." - ".$SQLCommand, "1");
                if(!isset($_SESSION["MYSQL_ERR"])){
                    //session_register("MYSQL_ERR");
                }
                $_SESSION["MYSQL_ERR"]=$errmsg;
                $JO_MySQL->close();
                return 0;
            }
        } catch (Exception $exc) {
            $errmsg= "<b style='color:red'>Begin Dataprovider Error:</b><br/>";
            $errmsg.= "SQLCommand:<span style='color:#FFAC30;'>".$SQLCommand."</span><br/>";
            $errmsg.= $exc->getTraceAsString();
            $errmsg.= "<br/><b style='color:red'>End Dataprovider Error</b>";
            //echo "<a href='JO_00_integration.php?JO_unit=00_Error' style='color:red;' target='_blank'>Click Here To View This Error</a>";
            if(!isset($_SESSION["MYSQL_ERR"])){
                //session_register("MYSQL_ERR");
            }
            $_SESSION["MYSQL_ERR"]=$errmsg;
            $JO_MySQL->close();
            return 0;
            
        }
        $JO_MySQL->close();
        return $affected_rows;
    }
    

    public static function beginTran(){

        $query = "BEGIN;";
        DataProvider::executeSQL($query);
    }
    public static function confirmar(){

        $query = "COMMIT;";
        DataProvider::executeSQL($query);
    }
    public static function revertir(){
        $query = "ROLLBACK;";
        DataProvider::executeSQL($query);
    }
}
?>